/*
 License
 
 Copyright © 2012 Kevin Kimmich
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights to 
 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 the Software, and to permit persons to whom the Software is furnished to do so, 
 subject to the following conditions:
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 @file
 Exported methods and types for using the library to run a web server.
 */

#ifndef _kuato_server_h
#define _kuato_server_h

#ifdef __cplusplus
extern "C" {
#endif
	
#include "crtypes.h"
#include "kuato_http.h"
#include "kuato_types.h"
	
	/**
	 Initialize the http server object.
	 @param h The server object
	 * 
	 * Return 0 on success, -1 on failure.
	 */
	int http_server_init(http_server_object* h);
	
	/**
	 Bind the server to an address and a port, optionally use ssl.
	 @param h the server.
	 @param address the address to bind to, 0.0.0.0 means any.
	 @param port the port.
	 @param cert_file the certificate file--only for an SSL socket, leave NULL for non SSL
	 @param key_file the key file--only for an SSL socket, leave NULL for non SSL
	 * 
	 * Returns the listening context or NULL on failure.
	 */
	connection_context* http_server_bind( http_server_object* h, char* address, uint16_t port, const char* cert_file, const char* key_file );
	
	/**
	 Free the resources associated with the server object.
	 @param h the server object
	 */
	void http_server_cleanup(http_server_object* h);
	
	/**
	 Add a domain object to the server.
	 @param h the server object.
	 @param root the server root path.
	 @param default_file the default file to open.
	 @param name the domain name
	 @param aliases aliases for the domain.
	 */
	domain_object* http_server_add_domain(http_server_object* h,const char* root, const char* default_file, const char* name, const char** aliases);
	
	/**
	 Lookup a domain by host
	 @param h the server object
	 @param host_name The name of the host.
	 */
	domain_object* http_server_lookup_domain(http_server_object* h,const char* host_name);
	
	/**
	 Handle a new connection. This is a core_thread handler function.
	 @param argc the count of arguments.
	 @param argv The argument list.
	 */
	void* handle_http_connection(int argc, void** argv);
	
	/**
	 Write a chunk to a client. Terminate the request by writing zero length. If the connection is
	 closed, the function returns -1, otherwise, it returns 0. If the call returns -1, the chunk handler
	 should exit.
	 @param m The method.
	 @param length The length of the chunk.
	 @param data  Pointer to the buffer to send.
	 */
	int http_server_write_chunk( method* m, size_t length, void* data );
	
#ifdef __cplusplus
}
#endif


#endif

